package router

import (
	"log"

	"github.com/gin-gonic/gin"
	middlewares "github.com/smash-llj/Nexa-server/middleware"
	"github.com/spf13/viper"
)

type IFnRegisterRoute = func(rgPublic *gin.RouterGroup, tgAuth *gin.RouterGroup)

// 收集每个大模块 例如： test user
var gfnRoutes []IFnRegisterRoute

func InitRouter() {
	//gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	//区分全局鉴权路由
	rgPublic := r.Group("/public")
	rgAuth := r.Group("", middlewares.JwtAuth())

	//注册全部路由
	InitBasePlatformRoutes()

	//注册子路由
	for _, fnRegisterRoute := range gfnRoutes {
		fnRegisterRoute(rgPublic, rgAuth)
	}
	port := viper.GetString("server.port")
	log.Fatal(r.Run(":" + port))
}

// RegisterRoute 添加到gfnRoutes
func RegisterRoute(fn IFnRegisterRoute) {
	if fn == nil {
		return
	}
	gfnRoutes = append(gfnRoutes, fn)
}

func InitBasePlatformRoutes() {
	InitWebsocketRoutes()
	InitCaptchaRoutes()
	InitUserRoutes()
	InitSessionRoutes()
	InitFriendRoutes()
	InitGroupRoutes()
	InitFileRoutes()
	InitAiRouters()
}
